/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.model.Menu;
import com.cmcc.zysoft.infocollection.service.CompanyPcService;
import com.cmcc.zysoft.infocollection.service.MenuPcService;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: MenuPcController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-9 上午10:26:36
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("/pc/menu")
public class MenuController extends BaseController {

	/**
	 * 日志.
	 */
	private static Logger _logger = LoggerFactory.getLogger(MenuController.class); 
	
	/**
	 * 属性名称: menuPcService.
	 * 类       型: MenuPcService.
	 */
	@Resource
	private MenuPcService menuPcService;
	
	/**
	 * 属性名称: companyPcService.
	 * 类       型: CompanyPcService.
	 */
	@Resource
	private CompanyPcService companyPcService;
	
	/**
	 * 菜单管理页面.
	 * @return String
	 */
	@RequestMapping("/main.htm")
	public String main() {
		return "/pc/menu/list";
	}
	
	/**
	 * 菜单树.
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/tree.htm")
	@ResponseBody
	public List<Map<String, Object>> menuTree() {
		return this.menuPcService.menuTree();
	}
	
	/**
	 * 添加菜单页面.
	 * @return String
	 */
	@RequestMapping("/add.htm")
	public String add() {
		return "/pc/menu/add";
	}
	
	/**
	 * 编辑菜单信息页面.
	 * @param modelMap modelMap
	 * @param menuId 菜单ID
	 * @return String
	 */
	@RequestMapping("/edit.htm")
	public String add(ModelMap modelMap, String menuId) {
		Menu menu = this.menuPcService.getEntity(menuId);
		modelMap.addAttribute("menu", menu);
		return "/pc/menu/edit";
	}
	
	/**
	 * 添加菜单信息.
	 * @param menu 菜单信息
	 * @return String
	 */
	@RequestMapping("/saveMenuInfo.htm")
	@ResponseBody
	@Transactional
	public String saveMenuInfo(Menu menu) {
		if(!"0".equals(menu.getParentId())) {
			Menu parentMenu = this.menuPcService.getEntity(menu.getParentId());
			parentMenu.setIsLeaf("N");
			this.menuPcService.updateEntity(parentMenu);
		}
		menu.setIsLeaf("Y");
		this.menuPcService.insertEntity(menu);
		return "SUCCESS";
	}
	
	/**
	 * 更新菜单信息.
	 * @param menu 菜单信息
	 * @return String
	 */
	@RequestMapping("/updateMenuInfo.htm")
	@ResponseBody
	@Transactional
	public String updateMenuInfo(Menu menu) {
		if(!"0".equals(menu.getParentId())) {
			Menu parentMenu = this.menuPcService.getEntity(menu.getParentId());
			parentMenu.setIsLeaf("N");
			this.menuPcService.updateEntity(parentMenu);
		}
		Menu newMenu = this.menuPcService.getEntity(menu.getMenuId());
		if(!newMenu.getParentId().equals(menu.getParentId())) {
			List<Menu> list = this.menuPcService.findByNamedParam(new String[]{"parentId", "status"}, 
					new Object[]{newMenu.getParentId(), "1"});
			if(null != list && list.size() == 1) {
				Menu newParentMenu = this.menuPcService.getEntity(newMenu.getParentId());
				newParentMenu.setIsLeaf("Y");
				this.menuPcService.updateEntity(newParentMenu);
			}
		}
		newMenu.setPath(menu.getPath());
		newMenu.setMenuName(menu.getMenuName());
		newMenu.setParentId(menu.getParentId());
		newMenu.setCompanyId(menu.getCompanyId());
		newMenu.setStatus(menu.getStatus());
		newMenu.setDisplayOrder(menu.getDisplayOrder());
		this.menuPcService.updateEntity(newMenu);
		return "SUCCESS";
	}
	
	/**
	 * 菜单详情页面.
	 * @param modelMap modelMap
	 * @param menuId 菜单ID
	 * @return String
	 */
	@RequestMapping("/detail.htm")
	public String detail(ModelMap modelMap, String menuId) {
		Menu menu = this.menuPcService.getEntity(menuId);
		modelMap.addAttribute("menu", menu);
		return "/pc/menu/detail";
	}
	
	/**
	 * 组合树-用户新增、修改菜单信息.
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/comboTree.htm")
	@ResponseBody
	public List<Map<String, Object>> comboTree() {
		List<Map<String, Object>> list = this.menuPcService.menuTree();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", "0");
		map.put("text", "无");
		list.add(map);
		return list;
	}
	
	/**
	 * 通过菜单ID查找企业ID.
	 * @param menuId 菜单ID
	 * @return String
	 */
	@RequestMapping("/getCompIdByMenuId.htm")
	@ResponseBody
	public String getCompIdByMenuId(String menuId) {
		Menu menu = this.menuPcService.getEntity(menuId);
		return menu.getCompanyId();
	}
	
	/**
	 * 根据菜单名称搜索菜单.
	 * @param menuName 菜单名称
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/search.htm")
	@ResponseBody
	@Transactional
	public List<Map<String, Object>> searchDept(String menuName){
		return this.menuPcService.searchMenu(menuName);
	}
	
	/**
	 * 删除菜单.
	 * @param menuIds 菜单ID集合
	 * @return String
	 */
	@RequestMapping("/delete.htm")
	@ResponseBody
	@Transactional
	public String delete(String menuIds) {
		String[] ids = menuIds.split(",");
		for(int i = 0; i < ids.length; i++) {
			this.menuPcService.deleteMenu(ids[i]);
		}
		return "SUCCESS";
	}
	
	/**
	 * 初始化顶部菜单.
	 * @return List<Menu>
	 */
	@RequestMapping("/topmenu.htm")
	@ResponseBody
	public List<Menu> initTopMenu() {
		_logger.debug("初始化顶部菜单");
		return this.menuPcService.initTopMenu();
	}
	
	/**
	 * 根据父菜单ID获取其子菜单.
	 * @param pid 父菜单ID
	 * @return List<Menu>
	 */
	@RequestMapping("/submenu/{pid}.htm")
	@ResponseBody
	public List<Menu> getMenus(@PathVariable String pid) {
		_logger.debug("获取菜单ID={}的子菜单", pid);
		List<Menu> menus = this.menuPcService.getMenusByPid(pid);
		return menus;
	}
	
	/**
	 * 过滤session过期.
	 * @param menuId 菜单ID
	 * @param url 菜单URL
	 * @return Map<String, String>
	 */
	@RequestMapping("/filter.htm")
	@ResponseBody
	public Map<String, String> filterSessionTimeout(String menuId, String url) {
		_logger.debug("#过滤session过期{}", url);
		Map<String, String> map = new HashMap<String, String>();
		map.put("menuId", menuId);
		map.put("url", url);
		return map;
	}
}